import { JXDef } from '../../../conventions/JXCommon';
import GameMgr from '../../Logic/GameMgr';
import { INVALID_VALUE } from './../../../Core/CoreDefine';
import { ObjectWrap } from './../../../Core/FrameEx/ES5Ex';
import { JXBtlCamp } from './JXBattleDefine';
import JXBtlMgr from './JXBattleManager';
import JXRBCmdMgr from './JXRBCmdMgr';

export default class JXBtlMonsterFactory extends ObjectWrap {
	/**每個怪物的信息 */
	protected _monsterBrithInfo: any[] = [];
	/**路径信息 */
	protected _roadInfo: any[] = [];
	/**定时器 */
	protected timer: any = null;
	/**最大波数 */
	protected maxWave: number = INVALID_VALUE;
	/**每一只怪物生成的间隔时间 */
	protected _monsterInterval: number = INVALID_VALUE;
	/**每一波怪物生成的间隔时间 */
	protected _teamInterval: number = INVALID_VALUE;
	/**命令管理 */
	protected _cmd: JXRBCmdMgr = null;
	/**生产怪物结束 */
	protected _produceEnd: boolean = false;
	public get isProduceEnd() {
		return this._produceEnd;
	}

	protected _isProduce: boolean = false;
	public get isProduce() {
		return this._isProduce;
	}

	private _tweens: cc.Tween<cc.Node>[] = [];

	private _curIsOver: boolean = false;

	/**当前波数*/
	protected _waveNum: number = 0;
	public get waveNum() {
		return this._waveNum;
	}
	public init(monster: string[][], logic: number[][], btl: JXRBCmdMgr, tweens: cc.Tween[]) {
		this.maxWave = 0;
		this._tweens = tweens;
		this._monsterBrithInfo = monster;
		this._roadInfo = logic;
		this._cmd = btl;
		this._monsterBrithInfo.forEach((team: string) => {
			if (this.maxWave < team.length) {
				this.maxWave = team.length;
			}
		})
		this._waveNum = 0;
		this.timer = null;
		this._monsterInterval = GameMgr.systemConfig.value(JXDef.SYS_CONFIG_KEY.monsterInterval);
		this._teamInterval = GameMgr.systemConfig.value(JXDef.SYS_CONFIG_KEY.teamInterval);

		if (CC_DEV) {
			console.log("怪物数据初始化完成", this._monsterBrithInfo);
			console.log("路径数据初始化完成", this._roadInfo)
			console.log("怪物最大波数为", this.maxWave)
		}


	}

	public startRoadAni() {
		for (let i = 0; i < this._roadInfo.length; i++) {
			if (!this._monsterBrithInfo[i]) continue;
			let team: string[] = this._monsterBrithInfo[i][this._waveNum];
			if (team && team.length) {
				let tween = this._tweens[i];
				tween.start();
			}
		}
	}

	/**开始生产怪物 */
	public startProduce() {
		setTimeout(() => {
			this._isProduce = true;
		}, 2000)

		if (this._produceEnd) return;
		this._curIsOver = false;
		for (let i = 0; i < this._roadInfo.length; i++) {
			if (!this._monsterBrithInfo[i]) continue;
			let team: string[] = this._monsterBrithInfo[i][this._waveNum];
			if (!team) continue;
			let road: number[] = this._roadInfo[i];
			let role = team.shift();
			if (role) {
				this.produceRole(role, road);
				if (CC_DEV) {
					console.log(`出生点${i + 1},第${this._waveNum + 1}波怪物生成：${role}`);
				}

			}
			if (!this.checkAllTeamOver()) {
				if (!this.timer) {
					this.timer = setTimeout(() => {
						clearTimeout(this.timer)
						this.timer = null;
						this.startProduce();
					}, this._monsterInterval * 1000)

				}
			} else {
				clearTimeout(this.timer);
				this._curIsOver = true;
				this.timer = null;
			}
		}
	}

	public nextWave() {
		if (this._waveNum >= this.maxWave) {
			clearTimeout(this.timer)
			this.timer = null;
			this._produceEnd = true;
			console.log("怪物生成结束")
			return false;
		}
		if (!this._curIsOver) return false;
		this._waveNum++
		clearTimeout(this.timer);
		this.startRoadAni();
		this.timer = null;
		this.startProduce();
		return true;
	}

	protected checkAllTeamOver() {
		let isOver = true;
		for (let i = 0; i < this._roadInfo.length; i++) {
			if (!this._monsterBrithInfo[i]) continue;
			let team: string[] = this._monsterBrithInfo[i][this._waveNum];
			if (team && team.length) {
				isOver = false;
				break;
			}
		}
		return isOver;
	}
	/**生产role */
	protected produceRole(roleId: string, road: number[]) {
		let hero = JXBtlMgr.ins.getTeamByCamp(JXBtlCamp.Attacker).goBattle(roleId)
		if (hero) {
			this._cmd.createRole(hero.heroId, hero.index, JXBtlCamp.Attacker, road);
		}
	}

	public destory() {
		clearTimeout(this.timer)
		this._produceEnd = false;
		this._isProduce = false;
		this.timer = null;
		this._waveNum = 0;
		this.maxWave = 0;
	}
}
